package com.luckku.boot.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luckku.boot.common.api.ApiResult;
import com.luckku.boot.entity.Role;
import com.luckku.boot.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Luckku
 * @since 2022-11-08
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping
    public ApiResult<?> save(@RequestBody Role role) {
        return ApiResult.success(roleService.save(role));
    }

    @PutMapping
    public ApiResult<?> update(@RequestBody Role role) {
        return ApiResult.success(roleService.updateById(role));
    }

    @DeleteMapping("/{id}")
    public ApiResult<?> delete(@PathVariable Long id) {
        Role role = roleService.getById(id);

        roleService.removeById(id);
        return ApiResult.success();
    }

    @GetMapping("/{id}")
    public ApiResult<Role> findById(@PathVariable Long id) {
        return ApiResult.success(roleService.getById(id));
    }

    @GetMapping
    public ApiResult<List<Role>> findAll() {
        return ApiResult.success(roleService.list());
    }

    @GetMapping("/page")
    public ApiResult<IPage<Role>> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                           @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return ApiResult.success(roleService.page(new Page<>(pageNum, pageSize), Wrappers.<Role>lambdaQuery().like(Role::getName, name)));
    }
}

